// pages/viode/viode.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //定义一个字符串储存id
    viodeId: '',
    //储存获取视频详情的值
    userfamliy: {},
    //点击评论将部分隐藏
    hiddenenit: false,
    //品论部分的显示隐藏
    hiddenends: true,
    //将点赞图标隐藏,灰色以及有色
    hiddencss:true,
    hiddencsser:false,
    //储存评论列表的值
    cdddata: {},
    //储存评论的id
    tosodata: ''
  },

  //点击简介显示部分
  bankup: function () {
    this.setData({
      hiddenenit: false,
      hiddenends: true,
    })
  },

  //点击评论隐藏简介部分
  updowns: function () {
    this.setData({
      hiddenenit: !this.data.hiddenenit,
      hiddenends: !this.data.hiddenends
    })
    //将缓存的数据取出来，把token传过去
    var item = wx.getStorageSync('key');
    console.log(item);
    console.log(this.data.viodeId);
    // 页面加载获取品论列表
    wx.request({
      url: `http://127.0.0.1:7001/api/v1/videos/${this.data.viodeId}/comments`,
      data: { videoId: this.data.viodeId },
      header: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': item.token, },
      method: 'GET',
      success: (result) => {
        console.log(result);
        if (result.statusCode == 200) {
          wx.showToast({
            title: '获取成功',
          });
          //进行赋值评论详情列表和评论id
          this.setData({
            cdddata: result.data.comments,
            tosodata: result.data.comments[0]._id
          })
          console.log(this.data.tosodata);
        } else {
          wx.showToast({
            title: '获取失败',
          });
        }
      },
    });
  },

  //点击提交进行添加评论
  tijiaoform: function (data) {
    console.log(this.data.viodeId);
    //打印提交的input框的值
    console.log(data.detail.value.comments);
    //将缓存的数据取出来，把token传过去
    var item = wx.getStorageSync('key');
    console.log(item);
    //发起请求,傳入token和视频的id
    wx.request({
      url: `http://127.0.0.1:7001/api/v1/videos/${this.data.viodeId}/comments`,
      data: { content: data.detail.value.comments },
      header: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': item.token, },
      method: 'POST',
      success: (result) => {
        console.log(result);
        //判断状态
        if (result.statusCode == 200) {
          wx.showToast({
            title: "评论成功"
          })
          //调用刷新函数，进行刷新
          this.updowns();
        } else {
          wx.showToast({
            title: "评论失败"
          })
        }
      },
    });
  },

  //点击删除评论
  out: function (env) {
    //获取评论的id
    console.log(env.currentTarget.dataset.key);
    //将缓存的数据取出来，把token传过去
    var value = wx.getStorageSync('key');
    console.log(value);
    // 发去请求
    wx.request({
      url: `http://127.0.0.1:7001/api/v1/videos/${this.data.viodeId}/comments/${env.currentTarget.dataset.key}`,
      data: { videoId: this.data.viodeId, commentId: this.data.tosodata },
      header: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': value.token, },
      method: 'DELETE',
      success: (result) => {
        console.log(result);
        if (result.statusCode == 204) {
          wx.showModal({
            title: '您确定要删除吗？',
          })
          //调用刷新函数，进行刷新
          this.updowns();
        } else {
          wx.showToast({
            title: "删除失败"
          })
        }
      },
    });
  },

  //点赞
  goods: function () {
    //将点赞图标显示
    this.setData({
      hiddencss:!this.data.hiddencss,
      hiddencsser: !this.data.hiddencsser,
    })
    //将缓存的数据取出来，把token传过去
    var item = wx.getStorageSync('key');
    console.log(item);
    //发球请求
    wx.request({
      url: `http://127.0.0.1:7001/api/v1/videos/${this.data.viodeId}/like`,
      data: { videoId: this.data.viodeId },
      header: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': item.token, },
      method: 'POST',
      success: (result) => {
        console.log(result);
        //判断状态
        if (result.statusCode == 200) {
          wx.showToast({
            title: '感谢点赞',
          });
          //调用刷新函数，进行刷新
          this.updowns();
        } else {
          wx.showToast({
            title: '点赞失败'
          })
        }
      },
    });
  },

  //取消点赞
  gooder:function(){
    //将未点赞图标显示
    this.setData({
      hiddencss:true,
      hiddencsser: false,
    })
    //将缓存的数据取出来，把token传过去
    var item = wx.getStorageSync('key');
    console.log(item);
    wx.request({
      url: `http://127.0.0.1:7001/api/v1/videos/${this.data.viodeId}/dislike`,
      data: {videoId: this.data.viodeId},
      header: {'content-type': 'application/x-www-form-urlencoded', 'Authorization': item.token,},
      method: 'POST',
      success: (result)=>{
        console.log(result);
        if(result.statusCode == 200){
          wx.showToast({
            title:"取消成功"
          })
          //调用刷新函数，进行刷新
          this.updowns();
        }else{
          wx.showToast({
            title: '取消点赞失败'
          })
        }
      },
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    //将缓存的数据取出来，把token传过去
    var item = wx.getStorageSync('key');
    console.log(item);
    var that = this;
    console.log(options.id);
    //页面加载发起请求获取视频详情
    wx.request({
      url: 'http://127.0.0.1:7001/api/v1/video/' + options.id,
      data: { videoId: options.id },
      header: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': item.token, },
      method: 'GET',
      success: (result) => {
        that.setData({
          viodeId: "624108fc1cd8ed222cfd3cec"
        })
        console.log(this.data.viodeId);
        // console.log(result);
        if (result.statusCode == 200) {
          wx.showToast({
            title: '获取成功',
          });
          //请求成功之后进行存储值，还有视频id
          that.setData({
            userfamliy: result.data.videoInfo,
            viodeId: result.data.videoInfo._id
          })
         
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})